package com.util;

import com.Constants;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;

public class Util {
	public static final ObjectMapper MAPPER = new ObjectMapper(); 
	public static final String INT_IP = "^(1[0-9][0-9])|(2[0-4][0-9])|(25[0-5])|([1-9][0-9])|([0-9])$";
	
	public static boolean checkIP(String ip){
		if (null == ip) {
			return false;
		}
//		if ("localhost".equals(ip)) {
//			return true;
//		}
		// IPv4
		if (isIPv4(ip)) {
			return true;
		}
		// IPv6
		
		return false;
	}
	
	private static boolean isIPv4(String ip){
		String[] str = ip.split("\\.");
		if (4 == str.length) {
			for (String string : str) {
				if (!string.matches(INT_IP)) {
					return false;
				}
			}
			return true;
		} else {
			return false;
		}
	}
	
	public static String toJson(Object obj) {
		if (null != obj) {
			try {
				return MAPPER.writeValueAsString(obj);
			} catch (Exception e) {
//				e.printStackTrace();
			}
		}
		return Constants.STRING_EMPTY;
	}
	public static <T> T toBean(String json,Class<T> clazz) {
		if (null == json) {
			return null;
		}
		try {
			MAPPER.configure(DeserializationFeature.FAIL_ON_INVALID_SUBTYPE, false);
			return MAPPER.readValue(json, clazz);
		} catch (Exception e) {
			return null;
		}
	}
	/**
	 * <br>是数字
	 * @param string
	 * @return
	 */
	public static boolean isDigit(String string) {
		if (null != string) {
			if (string.startsWith("+")) {
				return string.matches("^\\+\\d+(\\.\\d+)?$");
			} else if (string.startsWith("-")) {
				return string.matches("^\\-\\d+(\\.\\d+)?$");
			} else {
				return string.matches("^\\d+(\\.\\d+)?$");
			}
		}
		return false;
	}
	/**
	 * <br>是整数
	 * @param string
	 * @return
	 */
	public static boolean isInteger(String string) {
		if (null != string) {
			if (string.startsWith("+")) {
				return string.matches("^\\+[1-9][0-9]*$");
			} else if (string.startsWith("-")) {
				return string.matches("^\\-[1-9][0-9]*$");
			} else {
				return string.matches("^[0-9]+$");
			}
		}
		return false;
	}
	/**
	 * <br>是自然数
	 * @param string
	 * @return
	 */
	public static boolean isNaturalNumber(String string) {
		if (null != string) {
			if (string.startsWith("+")) {
				return string.matches("^\\+[1-9][0-9]*$");
			} else if (string.startsWith("-")) {
				return string.matches("^\\-[0]+$");
			} else {
				return string.matches("^[0-9]+$");
			}
		}
		return false;
	}
	/**
	 * <br>是正整数
	 * @param string
	 * @return
	 */
	public static boolean isPositiveInteger(String string) {
		return null==string? false : 
			(string.startsWith("+")?string.matches("^\\+[1-9][0-9]*$"):string.matches("^[1-9][0-9]*$"));
	}
	/**
	 * <br>是负整数
	 * @param string
	 * @return
	 */
	public static boolean isNegtiveInteger(String string) {
		return null==string? false : string.matches("^\\-[1-9][0-9]*$");
	}
	/**
	 * <br>是整数，非正。
	 * @param string
	 * @return
	 */
	public static boolean isNonPositiveInteger(String string) {
		if (null != string) {
			if (string.startsWith("+")) {
				return string.matches("^\\+[0]+$");
			} else if (string.startsWith("-")) {
				return string.matches("^\\-[1-9][0-9]*$");
			} else {
				return "0".equals(string);
			}
		}
		return false;
	}
	/**
	 * <br>是端口号
	 * @param port
	 * @return
	 */
	public static boolean isPort(String port) {
		return isNaturalNumber(port)?isPort(Integer.parseInt(port)):false;
	}
	/**
	 * <br>是端口号
	 * @param port
	 * @return
	 */
	public static boolean isPort(Integer port) {
		return (port>=0 && port<=65535);
	}
}
